/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.generator.controller;

import net.db.generator.JavaBaseClassWriter;
import net.db.generator.utils.ClassGeneratorUtil;

/**
 *
 * @author abosancic
 */
public class JSFControllerWriter extends JavaBaseClassWriter {

    public JSFControllerWriter() {
        super();
    }

    @Override
    protected void generateHeader() {
        super.generateHeader(); //To change body of generated methods, choose Tools | Templates.
        stringClass.append("@ManagedBean(name = \"" + className + "Controller\")\n" +
                           "@SessionScoped\n");
    }

    @Override
    protected void generateClassName() {
        stringClass.append("public class " + ClassGeneratorUtil.humanize(getClassName()) + " extends BaseController<" + ClassGeneratorUtil.humanize(className) + "> implements Serializable {\n\n");
        generateDAOClass();
    }

    //==========================================================================
    // setters and getters
    //==========================================================================
    @Override
    public String getClassName() {
        return className + "Controller";
    }

    protected void generateDAOClass() {

        String daoClass = "    private DAO" + ClassGeneratorUtil.humanize(className) + " mngr;\n"
                + "    \n"
                + "    public " + ClassGeneratorUtil.humanize(getClassName()) + "() {\n"
                + "    }\n"
                + "    \n"
                + "    @Override\n"
                + "    protected DAO" + ClassGeneratorUtil.humanize(className) + " getFacade() {\n"
                + "        if (mngr == null) {\n"
                + "            mngr = new DAO" + ClassGeneratorUtil.humanize(className) + "();\n"
                + "        }\n"
                + "        return mngr;\n"
                + "    }\n"
                + "    \n"
                + "    @Override\n"
                + "    protected Class getClassType(){\n"
                + "        return " + ClassGeneratorUtil.humanize(className) + ".class;\n"
                + "    }\n"
                + "\n"
                + "    @Override\n"
                + "    public String create() {\n"
                + "        try {\n"
                + "            getFacade().save(selected);\n"
                + "            JsfUtil.addSuccessMessage(ResourceBundle.getBundle(\"/Bundle\").getString(\"" + ClassGeneratorUtil.humanize(className) + "Created\"));\n"
                + "            return prepareCreate();\n"
                + "        } catch (Exception e) {\n"
                + "            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle(\"/Bundle\").getString(\"PersistenceErrorOccured\"));\n"
                + "            return null;\n"
                + "        }\n"
                + "    }\n"
                + "\n"
                + "    public String update() {\n"
                + "        try {\n"
                + "            getFacade().update(selected);\n"
                + "            JsfUtil.addSuccessMessage(ResourceBundle.getBundle(\"/Bundle\").getString(\"" + ClassGeneratorUtil.humanize(className) + "Updated\"));\n"
                + "            return \"View\";\n"
                + "        } catch (Exception e) {\n"
                + "            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle(\"/Bundle\").getString(\"PersistenceErrorOccured\"));\n"
                + "            return null;\n"
                + "        }\n"
                + "    }\n"
                + "\n"
                + "    @Override\n"
                + "    protected void performDestroy() {\n"
                + "        try {\n"
                + "            getFacade().remove(selected);\n"
                + "            JsfUtil.addSuccessMessage(ResourceBundle.getBundle(\"/Bundle\").getString(\"" + ClassGeneratorUtil.humanize(className) + "Deleted\"));\n"
                + "        } catch (Exception e) {\n"
                + "            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle(\"/Bundle\").getString(\"PersistenceErrorOccured\"));\n"
                + "        }\n"
                + "    }\n"
//                + "    @FacesConverter(forClass = " + ClassGeneratorUtil.humanize(className) + ".class)\n"
//                + "    public static class " + ClassGeneratorUtil.humanize(className) + "ControllerConverter extends BaseControllerConverter{\n"
//                + "\n"
//                + "        @Override\n"
//                + "        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {\n"
//                + "            if (value == null || value.length() == 0) {\n"
//                + "                return null;\n"
//                + "            }\n"
//                + "            " + ClassGeneratorUtil.humanize(className) + "Controller controller = (" + ClassGeneratorUtil.humanize(className) + "Controller) facesContext.getApplication().getELResolver().\n"
//                + "                    getValue(facesContext.getELContext(), null, \"" + className + "Controller\");\n"
//                + "            return controller.mngr.find(getKey(value));\n"
//                + "        }\n"
//                + "\n"
//                + "        @Override\n"
//                + "        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {\n"
//                + "            if (object == null) {\n"
//                + "                return null;\n"
//                + "            }\n"
//                + "            if (object instanceof " + ClassGeneratorUtil.humanize(className) + ") {\n"
//                + "                " + ClassGeneratorUtil.humanize(className) + " o = (" + ClassGeneratorUtil.humanize(className) + ") object;\n"
//                + "                return getStringKey(o." + ClassGeneratorUtil.humanizeToGetter(uniqueField) + "());\n"
//                + "            } else {\n"
//                + "                throw new IllegalArgumentException(\"object \" + object + \" is of type \" + object.getClass().getName() + \"; expected type: \" + " + ClassGeneratorUtil.humanize(className) + ".class.getName());\n"
//                + "            }\n"
//                + "        }\n"
                + "    \n";
        stringClass.append(daoClass);
    }

    @Override
    protected void generateFields() {
    }

    @Override
    protected void generateToString() {
    }

    @Override
    protected void generateHashCode() {
    }

    @Override
    protected void generateEquals() {
    }

    @Override
    protected void generateConstructor() {
    }

    @Override
    protected void generateGettersAndSetters() {
    }
}
